package com.example.utils;

import com.example.domain.UserDetail;
import com.example.domain.dto.SysUserDto;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author ljs
 * @date 2025/7/26
 */
@Component
public class SecurityUtil {

    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public UserDetail getUserDetail() {
        Authentication authentication = getAuthentication();
        if (authentication != null) {
            UserDetail principal = (UserDetail) authentication.getPrincipal();
            return principal;
        }
        return null;
    }

    public SysUserDto getSysUserDto() {
        return getUserDetail().getSysUser();
    }

    public Long getUserId() {
        return getSysUserDto().getUserId();
    }
}
